/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary_package;


import Entitys.Persona;
import Entitys.Cargo;
import Helpers.RegexFormatter;
import Helpers.RegularExpression;
import control_package.RegisterControl;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import services.CargoServices;

/**
 *
 * @author Administrador
 */
public class RegisterGui extends javax.swing.JPanel {
    private RegisterControl regcon;
    private DefaultComboBoxModel RolComboboxModel=new DefaultComboBoxModel();
    private MainFrame frame;
    CargoServices carS=new CargoServices(); 
    /**
     * Creates new form RegisterGui
     */
    public RegisterGui(MainFrame frame) {
       
        this.setSize(350,350);
        initComponents();
        RolComboBoxModelFill();
        this.frame=frame;
        integrity();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel11 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        mailHostFormattedTextField = new javax.swing.JFormattedTextField();
        AdressJTFField = new javax.swing.JFormattedTextField();
        mailNameFormattedTextField = new javax.swing.JFormattedTextField();
        nameFormattedTextField = new javax.swing.JFormattedTextField();
        borrarButton2 = new javax.swing.JButton();
        aceptarButton = new javax.swing.JButton();
        pass2Field = new javax.swing.JPasswordField();
        passwordJTFField = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        LNameFormattedTextField = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        PhoneJTF = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        IdentificationJTFField = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        RolComboBox = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();

        jLabel11.setText("jLabel11");

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "REGISTRAR", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 18))); // NOI18N
        setPreferredSize(new java.awt.Dimension(1240, 640));

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel2.setText("@");

        mailHostFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        mailHostFormattedTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mailHostFormattedTextFieldActionPerformed(evt);
            }
        });

        AdressJTFField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        mailNameFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        mailNameFormattedTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mailNameFormattedTextFieldActionPerformed(evt);
            }
        });

        nameFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        borrarButton2.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        borrarButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/cross.png"))); // NOI18N
        borrarButton2.setText("BORRAR");
        borrarButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarButton2ActionPerformed(evt);
            }
        });

        aceptarButton.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        aceptarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/Copia de icon_check.png"))); // NOI18N
        aceptarButton.setText("ACEPTAR");
        aceptarButton.setPreferredSize(new java.awt.Dimension(123, 70));
        aceptarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarButtonActionPerformed(evt);
            }
        });

        pass2Field.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        passwordJTFField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        jLabel7.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel7.setText("Repetir contraseña");

        jLabel6.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel6.setText("Contraseña ");

        jLabel5.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel5.setText("Correo ");

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel3.setText("Dirección ");

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel1.setText("Nombre ");

        LNameFormattedTextField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel8.setText("Apellido ");

        jLabel9.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel9.setText("Teléfono ");

        PhoneJTF.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        jLabel4.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel4.setText("Identificacion ");

        IdentificationJTFField.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N

        jLabel10.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel10.setText("Rol");

        RolComboBox.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        RolComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        RolComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RolComboBoxActionPerformed(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N
        jLabel12.setText("Todos los campos son obligatorios");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel10)
                            .addComponent(jLabel6))
                        .addGap(71, 71, 71)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(IdentificationJTFField)
                            .addComponent(PhoneJTF)
                            .addComponent(LNameFormattedTextField)
                            .addComponent(pass2Field)
                            .addComponent(passwordJTFField)
                            .addComponent(nameFormattedTextField)
                            .addComponent(AdressJTFField)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(RolComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(mailNameFormattedTextField)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(mailHostFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 556, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(borrarButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(aceptarButton, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35))
                    .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, 1218, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nameFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(LNameFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(IdentificationJTFField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(AdressJTFField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(PhoneJTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(mailNameFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mailHostFormattedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(passwordJTFField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(pass2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(RolComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(27, 27, 27)
                .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(borrarButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(aceptarButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(56, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void borrarButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarButton2ActionPerformed
        cleanFields();
        
    }//GEN-LAST:event_borrarButton2ActionPerformed

    private void aceptarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarButtonActionPerformed
    if(EmptyFields()){
        JOptionPane.showMessageDialog(null, "Algún campo vacío","Mensaje", JOptionPane.OK_OPTION);
    }else if(!verifyPass()){
        JOptionPane.showMessageDialog(null, "Contraseña errada","Mensaje", JOptionPane.OK_OPTION);   
    }else{
        
        regcon=new RegisterControl();
          
          
          Persona pers= new Persona(new Long(Long.parseLong(this.IdentificationJTFField.getText())));
          pers.setNombrePersona(this.nameFormattedTextField.getText());
          pers.setIdDrogueria(MainFrame.DrugPresent.getIdDrogueria());
          pers.setApellidoPersona(this.LNameFormattedTextField.getText());
          pers.setTelefonoPersona(this.PhoneJTF.getText());
          pers.setMailPersona(this.mailNameFormattedTextField.getText()+"@"+this.mailHostFormattedTextField.getText());
          pers.setDireccionPersona(this.AdressJTFField.getText());
          pers.setPassPersona(this.passwordJTFField.getText());
          String cargo=(String) this.RolComboBox.getSelectedItem();
          System.out.println(RolComboBox.getSelectedItem());
          pers.setIdCargo(carS.ReadByType(cargo).getIdCargo());
          JOptionPane.showMessageDialog(null, regcon.makeRegister(pers),"Mensaje", JOptionPane.OK_OPTION);
          cleanFields();
            
    }
    
    
    
    }//GEN-LAST:event_aceptarButtonActionPerformed

  
    
    private void mailNameFormattedTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mailNameFormattedTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mailNameFormattedTextFieldActionPerformed

    private void RolComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RolComboBoxActionPerformed
        
    }//GEN-LAST:event_RolComboBoxActionPerformed

    private void mailHostFormattedTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mailHostFormattedTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mailHostFormattedTextFieldActionPerformed
    
    public void RolComboBoxModelFill(){
        
           this.RolComboboxModel.addElement("Seleccione");
           for(Cargo car:carS.readAll()){
           this.RolComboboxModel.addElement(car.getTipoCargo());
           }
           RolComboBox.setModel(RolComboboxModel);
           
    }
    
    public boolean verifyPass(){
     return this.passwordJTFField.getText().equals(this.pass2Field.getText());
    }
   
    public boolean EmptyFields(){
     if(this.nameFormattedTextField.getText().equals("")) return true;
     if(this.LNameFormattedTextField.getText().equals("")) return true;
     if(this.IdentificationJTFField.getText().equals("")) return true;
     if(this.AdressJTFField.getText().equals("")) return true;
     if(this.PhoneJTF.getText().equals("")) return true;
     if(this.mailNameFormattedTextField.getText().equals("")) return true;
     if(this.mailHostFormattedTextField.getText().equals("")) return true;
     if(this.passwordJTFField.getText().equals("")) return true;
     if(this.pass2Field.getText().equals("")) return true;
     if(this.RolComboBox.getSelectedItem().equals("Seleccione:")) return true;
     
    
        return false;
    
    }
    
    private void cleanFields(){
        this.nameFormattedTextField.setText("");
        this.LNameFormattedTextField.setText("");
        this.IdentificationJTFField.setText("");
        this.AdressJTFField.setText("");
        this.PhoneJTF.setText("");
        this.mailNameFormattedTextField.setText("");
        this.mailHostFormattedTextField.setText("");
        this.passwordJTFField.setText("");
        this.pass2Field.setText("");
        this.RolComboBox.setSelectedIndex(0);
        
        
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField AdressJTFField;
    private javax.swing.JFormattedTextField IdentificationJTFField;
    private javax.swing.JFormattedTextField LNameFormattedTextField;
    private javax.swing.JFormattedTextField PhoneJTF;
    private javax.swing.JComboBox RolComboBox;
    private javax.swing.JButton aceptarButton;
    private javax.swing.JButton borrarButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JFormattedTextField mailHostFormattedTextField;
    private javax.swing.JFormattedTextField mailNameFormattedTextField;
    private javax.swing.JFormattedTextField nameFormattedTextField;
    private javax.swing.JPasswordField pass2Field;
    private javax.swing.JPasswordField passwordJTFField;
    // End of variables declaration//GEN-END:variables

    private void integrity() {
        new RegexFormatter(RegularExpression.ONE_WORD).install(nameFormattedTextField);
        new RegexFormatter(RegularExpression.ONE_WORD).install(LNameFormattedTextField);
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(IdentificationJTFField);
        new RegexFormatter(RegularExpression.NATURAL_NUMBERS).install(PhoneJTF);
//        new RegexFormatter(RegularExpression.ADRESS).install(AdressJTFField);
        new RegexFormatter(RegularExpression.EMAIL_NAME).install(mailNameFormattedTextField);
        new RegexFormatter(RegularExpression.EMAIL_HOST).install(mailHostFormattedTextField);
    }
}
